﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PloobsEngine.Material;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine;
using Etapa1.BezierExperiments;
using VTFTutorial;
using Microsoft.Xna.Framework;

namespace Etapa1.ModelDescription.Terrains.Deformable
{
    public class TerrainMorphShader : IShader
    {
        public TerrainMorphShader(Grid grid,Texture2D t1,Texture2D t2)
        {
            this.grid = grid;
            this.t1 = t1;
            this.t2 = t2;
        }
        Texture2D sandTexture, grassTexture, rockTexture, snowTexture;

        Texture2D t1;
        Texture2D t2;
        Texture2D morphedtex;
        Texture2D normals;
        Effect displacementShader;
        Effect terrainShader;
        Grid grid;
        RenderTarget2D morphed;
        RenderTarget2D normalsRt;

        public override void PreDrawPhase(PloobsEngine.SceneControl.IWorld world, PloobsEngine.Modelo.IModelo modelo, PloobsEngine.SceneControl.IRenderHelper render, PloobsEngine.Cameras.ICamera cam)
        {
            ///Morph
            EngineStuff.GraphicsDevice.SetRenderTarget(0, morphed);
            EngineStuff.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.TransparentBlack, 1, 0);

            displacementShader.CurrentTechnique = displacementShader.Techniques["TextureMorph"];
            displacementShader.Parameters["textureMap1"].SetValue(t1);
            displacementShader.Parameters["textureMap2"].SetValue(t2);
            displacementShader.Parameters["morphFactor"].SetValue((float)Math.Sin(EngineStuff.GetCurrentGameTime().TotalGameTime.TotalSeconds) * 0.5f + 0.5f);
            
            render.RenderTextureToFullScreenSpriteBatch(t1, displacementShader);
            EngineStuff.GraphicsDevice.SetRenderTarget(0, null);
            morphedtex = morphed.GetTexture();
            //morphedtex.Save("bla.png", ImageFileFormat.Png);

            ///CalculateNormals
            //EngineStuff.GraphicsDevice.SetRenderTarget(0, normalsRt);
            //EngineStuff.GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.TransparentBlack, 1, 0);
            //displacementShader.CurrentTechnique = displacementShader.Techniques["ComputeNormals"];              
            //render.RenderTextureToFullScreenSpriteBatch(morphedtex, displacementShader);
            //EngineStuff.GraphicsDevice.SetRenderTarget(0, null);
            //normals = normalsRt.GetTexture();
            //normals.Save("bla2.png", ImageFileFormat.Png);
        }

        public override void Draw(PloobsEngine.Modelo.IModelo modelo, PloobsEngine.SceneControl.IRenderHelper render, PloobsEngine.Cameras.ICamera cam)
        {
            terrainShader.Begin();

            EngineStuff.GraphicsDevice.RenderState.CullMode = CullMode.None;
            EngineStuff.GraphicsDevice.RenderState.DepthBufferEnable = true;

            terrainShader.Parameters["world"].SetValue(Matrix.Identity);
            terrainShader.Parameters["view"].SetValue(cam.View);
            terrainShader.Parameters["proj"].SetValue(cam.Projection);
            terrainShader.Parameters["maxHeight"].SetValue(256);
            terrainShader.Parameters["displacementMap"].SetValue(morphedtex);
            //terrainShader.Parameters["normalMap"].SetValue(normals);
            terrainShader.Parameters["sandMap"].SetValue(sandTexture);
            terrainShader.Parameters["grassMap"].SetValue(grassTexture);
            terrainShader.Parameters["rockMap"].SetValue(rockTexture);
            terrainShader.Parameters["snowMap"].SetValue(snowTexture);
            

            EngineStuff.GraphicsDevice.VertexDeclaration = grid.vertexDecl;
            EngineStuff.GraphicsDevice.Vertices[0].SetSource(grid.vb, 0, grid.vertexDecl.GetVertexStrideSize(0));
            EngineStuff.GraphicsDevice.Indices = grid.ib;

            terrainShader.CurrentTechnique.Passes[0].Begin();
            EngineStuff.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, (grid.Dimension + 1) * (grid.Dimension + 1), 0, grid.Dimension * grid.Dimension * 2);
            terrainShader.CurrentTechnique.Passes[0].End();
            terrainShader.End();

            EngineStuff.GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
        }

        public override MaterialType MaterialType
        {
            get { return MaterialType.DEFERRED; }
        }

        public override void Initialize()
        {
            terrainShader = EngineStuff.CustomContentManager.GetAsset<Effect>("Effects\\VTFDisplacement");
            displacementShader= EngineStuff.CustomContentManager.GetAsset<Effect>("Effects\\TextureMorph");
            morphed = EngineStuff.GetDefaultColorBuffer(256, 256);
            normalsRt = EngineStuff.GetDefaultColorBuffer(256, 256);
            sandTexture = EngineStuff.CustomContentManager.GetAsset<Texture2D>("Textures\\Morph\\sand");
            grassTexture = EngineStuff.CustomContentManager.GetAsset<Texture2D>("Textures\\Morph\\grass");
            rockTexture = EngineStuff.CustomContentManager.GetAsset<Texture2D>("Textures\\Morph\\rock");
            snowTexture = EngineStuff.CustomContentManager.GetAsset<Texture2D>("Textures\\Morph\\snow");
        }

    }
}
